package com.ywgt.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import com.ywgt.response.Result;
import com.ywgt.system.pojo.*;
import com.ywgt.system.service.ConsumerService;
import com.ywgt.system.service.OutStockService;
import com.ywgt.system.service.ProductService;
import com.ywgt.system.service.ProductStockService;
import com.ywgt.system.service.impl.OutStockInfoServiceImpl;
import com.ywgt.system.vo.OutStockVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yiping
 * @since 2021-01-22
 */
@RestController
@Api(value = "出库模块",tags = "出库模块")
@RequestMapping("/business/outStock")
public class OutStockController {

    @Autowired
    private OutStockService outStockService;

    @Autowired
    private OutStockInfoServiceImpl outStockInfoService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ConsumerService consumerService;

    @Autowired
    private ProductStockService productStockService;
    @ApiOperation(value = "添加出库单",notes = "出库")
    @PostMapping("/addOutStock")
    public Result add(@RequestBody OutStockVO outStockVO){
        AuthUser authUser = (AuthUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (!StringUtils.isEmpty(outStockVO.getConsumerId())){
            outStockService.consumerInsert(outStockVO,authUser,outStockVO.getConsumerId());
        }else {
            outStockService.newInsert(outStockVO,authUser);
        }
        return Result.ok();
    }
    @ApiOperation(value = "查询出库单",notes = "出库单列表查询")
    @GetMapping("/findOutStockList")
    public Result findOutStockList(@RequestParam(value = "pageNum",defaultValue = "1",required = false)Integer pageNum,
                                   @RequestParam(value = "pageSize",defaultValue = "8",required = false)Integer pageSize,
                                    OutStockVO outStockVO){
        Page<OutStockVO> outStockVOPage =outStockService.findOutStockList(pageNum,pageSize,outStockVO);
        List<OutStockVO> result = outStockVOPage.getResult();
        long total = outStockVOPage.getTotal();
        return Result.ok().data("rows",result).data("total",total);
    }

    @ApiOperation(value = "出库单详情",notes ="出库单详情查询")
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable("id")Long id,@RequestParam(value = "pageNum",defaultValue = "1",required = false)Integer pageNum,
                         @RequestParam(value ="pageSize" ,defaultValue = "3",required = false)Integer pageSize){
        OutStock outStock=outStockService.detail(id);
        if (outStock!=null) {
            Long consumerId = outStock.getConsumerId();
            Consumer consumer = consumerService.getById(consumerId);
            System.out.println(consumer);
            List<OutStockInfo> infoList = outStock.getInfoList();
            for (OutStockInfo outStockInfo : infoList) {
                outStockInfo.getProduct().setCount(outStockInfo.getProductNumber());
            }
            List<Product> products = infoList.stream().map(OutStockInfo::getProduct).collect(Collectors.toList());
            products = products.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
            return Result.ok().data("item", products).data("consumer", consumer).data("status", outStock.getStatus()).data("total", infoList.size());
        }
        return Result.error();
    }
    @ApiOperation(value = "出库单审核",notes = "出库单审核操作")
    @PutMapping("/publish/{id}")
    public Result publish(@PathVariable("id")Long id){
        LambdaUpdateWrapper<OutStock> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(OutStock::getId,id);
        updateWrapper.set(OutStock::getStatus,0);
        if (outStockService.update(updateWrapper)){
            return Result.ok();
        }
        return Result.error();
    }
    @ApiOperation(value = "出库单放入回收站",notes = "出库单放入回收站")
    @PutMapping("/remove/{id}")
    public Result remove(@PathVariable("id")Long id){
        LambdaUpdateWrapper<OutStock> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(OutStock::getId,id);
        updateWrapper.set(OutStock::getStatus,1);
        if (outStockService.update(updateWrapper)){
            return Result.ok();
        }
        return Result.error();
    }

    @ApiOperation(value = "出库回滚",notes = "出库单进行回滚操作")
    @PutMapping("/back/{id}")
    public Result back(@PathVariable("id")Long id){
        LambdaUpdateWrapper<OutStock> updateWrapper=new LambdaUpdateWrapper<>();
        updateWrapper.eq(OutStock::getId,id);
        updateWrapper.set(OutStock::getStatus,0);
        if (outStockService.update(updateWrapper)){
            return Result.ok();
        }
        return Result.error();
    }

    @ApiOperation(value = "出库单删除",notes = "出库单删除")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id")Long id){
        LambdaQueryWrapper<OutStock> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(OutStock::getId,id);
        OutStock outStock = outStockService.getOne(queryWrapper);
        LambdaQueryWrapper<OutStockInfo> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(OutStockInfo::getOutNum,outStock.getOutNum());
        if (outStockInfoService.remove(wrapper)){
            if (outStockService.removeById(id)){
                return Result.ok();
            }
        }
        return Result.error();
    }

    public LambdaQueryWrapper<OutStock> getWrapper(OutStock outStock){
        LambdaQueryWrapper<OutStock> queryWrapper=new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(outStock.getOutNum())){
            queryWrapper.like(OutStock::getOutNum,outStock.getOutNum());
        }
        if (!StringUtils.isEmpty(outStock.getStatus())){
            queryWrapper.like(OutStock::getStatus,outStock.getStatus());
        }
        if (!StringUtils.isEmpty(outStock.getType())){
            queryWrapper.like(OutStock::getType,outStock.getType());
        }
        return queryWrapper;
    }
}

